/**
 * 
 */
package org.xlab.baseframework.test.platform.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.Hashtable;

import org.xlab.baseframework.domain.enumerate.EnumDeletedIndentifying;

/**
 * @author MaKuBEX
 * 
 */
public class EpcCache implements Serializable {

	public class EpcInfo {
		private String epcNo;
		private String currentLocation;
		private EnumDeletedIndentifying deletedInd;
		private String lastUpdatedBy;
		private String createdBy;
		private Date lastUpdatedTime;
		private Date createdTime;
		private String lastOperation;

		/**
		 * @return the createdBy
		 */
		public String getCreatedBy() {
			return createdBy;
		}

		/**
		 * @return the createdTime
		 */
		public Date getCreatedTime() {
			return createdTime;
		}

		/**
		 * @return the currentLocation
		 */
		public String getCurrentLocation() {
			return currentLocation;
		}

		/**
		 * @return the deletedInd
		 */
		public EnumDeletedIndentifying getDeletedInd() {
			return deletedInd;
		}

		/**
		 * @return the epcNo
		 */
		public String getEpcNo() {
			return epcNo;
		}

		/**
		 * @return the lastOperation
		 */
		public String getLastOperation() {
			return lastOperation;
		}

		/**
		 * @return the lastUpdatedBy
		 */
		public String getLastUpdatedBy() {
			return lastUpdatedBy;
		}

		/**
		 * @return the lastUpdatedTime
		 */
		public Date getLastUpdatedTime() {
			return lastUpdatedTime;
		}

		/**
		 * @param createdBy
		 *            the createdBy to set
		 */
		public void setCreatedBy(String createdBy) {
			this.createdBy = createdBy;
		}

		/**
		 * @param createdTime
		 *            the createdTime to set
		 */
		public void setCreatedTime(Date createdTime) {
			this.createdTime = createdTime;
		}

		/**
		 * @param currentLocation
		 *            the currentLocation to set
		 */
		public void setCurrentLocation(String currentLocation) {
			this.currentLocation = currentLocation;
		}

		/**
		 * @param deletedInd
		 *            the deletedInd to set
		 */
		public void setDeletedInd(EnumDeletedIndentifying deletedInd) {
			this.deletedInd = deletedInd;
		}

		/**
		 * @param epcNo
		 *            the epcNo to set
		 */
		public void setEpcNo(String epcNo) {
			this.epcNo = epcNo;
		}

		/**
		 * @param lastOperation
		 *            the lastOperation to set
		 */
		public void setLastOperation(String lastOperation) {
			this.lastOperation = lastOperation;
		}

		/**
		 * @param lastUpdatedBy
		 *            the lastUpdatedBy to set
		 */
		public void setLastUpdatedBy(String lastUpdatedBy) {
			this.lastUpdatedBy = lastUpdatedBy;
		}

		/**
		 * @param lastUpdatedTime
		 *            the lastUpdatedTime to set
		 */
		public void setLastUpdatedTime(Date lastUpdatedTime) {
			this.lastUpdatedTime = lastUpdatedTime;
		}
	}
	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 1178684257161840558L;
	private static Hashtable<String, EpcInfo> inventory;

	private static EpcCache instance;

	public static EpcCache getInstance() {
		if (instance == null) {
			instance = new EpcCache();
			inventory = new Hashtable<String, EpcInfo>();
		}
		return instance;
	}

	private EpcCache() {
		super();
	}

	public synchronized void deleteEpcInfo(String epcNo, String operator,
			Date operationTime) {
		EpcInfo epcInfo = inventory.get(epcNo);
		epcInfo.setLastUpdatedTime(operationTime);
		epcInfo.setLastUpdatedBy(operator);
		epcInfo.setLastOperation("GI");
		epcInfo.setDeletedInd(EnumDeletedIndentifying.Y);
		inventory.put(epcNo, epcInfo);
	}

	/**
	 * @return the inventory
	 */
	public Hashtable<String, EpcInfo> getInventory() {
		return inventory;
	}

	public synchronized void saveOrUpdateEpcInfo(String epcNo, String location,
			String commandId, String operator, Date operationTime) {
		EpcInfo epcInfo = inventory.get(epcNo);
		if (epcInfo == null) {
			epcInfo = new EpcInfo();
			epcInfo.setEpcNo(epcNo);
			epcInfo.setCreatedTime(operationTime);
			epcInfo.setCreatedBy(operator);
		}
		epcInfo.setLastOperation(commandId);
		epcInfo.setLastUpdatedTime(operationTime);
		epcInfo.setLastUpdatedBy(operator);
		epcInfo.setCurrentLocation(location);
		epcInfo.setDeletedInd(EnumDeletedIndentifying.N);
		inventory.put(epcNo, epcInfo);
	}
}
